package models

import (
	"crypto/md5"
	"fmt"
	"strconv"
	"time"
)

type User struct {
	ID        int `gorm:"primaryKey`
	Name      string
	Email     string `gorm:"unique;not null"`
	Password  string `gorm:"not null"`
	CreatedAt time.Time
}

func (u *User) GetToken() string {
	data := md5.Sum([]byte(u.Email + strconv.Itoa(u.ID) + u.Password))
	return fmt.Sprintf("%x", data)
}

func (u *User) ValidateToken(token string) bool {
	return u.GetToken() == token
}

func (u *User) CreateThread(t *Thread) *Thread {
	t.User = *u
	Db.Create(t)
	return t
}
